<template>
  <div>
    <div class="bread">
      <div class="wrap">
        <nuxt-link to="/">
          <span>产品中心</span>
        </nuxt-link>
        <span class="arrow">＞</span>
        <span
          @click="
            goto(
              '../list/list?pname=' +
                encodeURIComponent(pname) +
                '&cname=' +
                encodeURIComponent(cname) +
                '&id=' +
                pid
            )
          "
        >
          {{ pname }}
        </span>
        <span class="arrow">＞</span>
        <span class="active">{{ cname }}</span>
      </div>
    </div>
    <div class="main" v-if="JSON.stringify(product) != '{}'">
      <div class="pic">
        <img
          :src="
            product.attrList[product.buyIndex].proPicList[product.showPicIndex]
          "
          class="big-pic"
        />
        <div class="nav">
          <img
            src="@/assets/imgs/arrow-left.png"
            class="left"
            @click="
              product.showPicIndex = Math.max(0, product.showPicIndex - 1)
              $forceUpdate()
            "
          />
          <div class="mid">
            <img
              class="item"
              :src="item"
              v-for="(item, oindex) in product.attrList[product.buyIndex]
                .proPicList"
              :class="oindex == product.showPicIndex ? 'active' : ''"
              :key="oindex"
              @click="
                product.showPicIndex = oindex
                $forceUpdate()
              "
            />
          </div>
          <img
            src="@/assets/imgs/arrow-right.png"
            class="right"
            @click="
              product.showPicIndex = Math.min(
                product.attrList[product.buyIndex].proPicList.length - 1,
                product.showPicIndex + 1
              )
              $forceUpdate()
            "
          />
        </div>
      </div>
      <div class="info">
        <div class="name">{{ product.name }}</div>
        <div class="memo">{{ product.detailsDescription }}</div>
        <div class="price">${{ product.attrList[product.buyIndex].price }}</div>
        <div class="line" />
        <div class="attrs">
          <div
            class="item"
            v-for="(item, index) in product.attrList[product.buyIndex]
              .productAttr"
            :key="index"
          >
            {{ item.name }}: {{ item.value }}
          </div>
        </div>
        <div class="line" />
        <div class="color-title">选择颜色：</div>
        <div class="colors">
          <div
            class="item"
            v-for="(item, index) in product.attrList"
            :class="product.buyIndex == index ? 'active' : ''"
            @click="selectAttr(index)"
            :key="index"
          >
            <img :src="product.attrList[index].proPicList[0]" class="pic" />
            {{ item.name }}
          </div>
        </div>
        <div class="dot-line" />
        <div class="buy">
          <div class="number">
            <div
              class="minus"
              :class="product.buyNumber == 1 ? 'disable' : ''"
              @click="
                product.buyNumber = Math.max(1, product.buyNumber - 1)
                $forceUpdate()
              "
            >
              -
            </div>
            <input
              type="number"
              :value="product.buyNumber"
              class="total"
              @input="onInputP"
            />
            <div
              class="plus"
              @click="
                product.buyNumber = product.buyNumber + 1
                $forceUpdate()
              "
            >
              +
            </div>
          </div>
          <div class="price-title">合计金额：</div>
          <div class="price">
            ${{ product.buyNumber * product.attrList[product.buyIndex].price }}
          </div>
          <div
            class="button"
            @click="
              addToCart(
                product.buyNumber,
                product.attrList[product.buyIndex].id,
                product.id
              )
            "
          >
            加入购物车
          </div>
        </div>
      </div>
      <div class="clear" />
      <div class="long-line" />
      <div class="offer-title">推荐的专业配件</div>
      <div class="offer-list" v-if="offerList.length > 0">
        <div class="item" v-for="(item, index) in offerList" :key="index">
          <img
            :src="item.attrList[item.buyIndex].proPicList[0]"
            class="pic"
            @click="
              goto(
                '../detail/detail?pname=' +
                  encodeURIComponent(pname) +
                  '&cname=' +
                  encodeURIComponent(cname) +
                  '&id=' +
                  item.id
              )
            "
            v-if="item.attrList.length > 0"
          />
          <img
            src="@/assets/imgs/nopic.png"
            class="pic"
            @click="
              goto(
                '../detail/detail?pname=' +
                  encodeURIComponent(pname) +
                  '&cname=' +
                  encodeURIComponent(cname) +
                  '&id=' +
                  item.id
              )
            "
            v-else
          />

          <div class="name">{{ item.name }}</div>
          <div class="price">
            {{
              item.attrList.length > 0
                ? '$' + item.attrList[item.buyIndex].promotionPrice
                : '暂无价格'
            }}
          </div>
          <select class="color">
            <option v-for="(option, oindex) in item.attrList">
              {{ option.name }}
            </option>
          </select>
          <div class="number">
            <div
              class="minus"
              :class="offerList[index].buyNumber == 1 ? 'disable' : ''"
              @click="
                offerList[index].buyNumber = Math.max(
                  1,
                  offerList[index].buyNumber - 1
                )
                $forceUpdate()
              "
            >
              -
            </div>
            <input
              type="number"
              :value="offerList[index].buyNumber"
              class="total"
              @input="onInput($event, index)"
            />
            <div
              class="plus"
              @click="
                offerList[index].buyNumber = offerList[index].buyNumber + 1
                $forceUpdate()
              "
            >
              +
            </div>
          </div>
          <div
            class="checkbox"
            :class="offerList[index].buyed ? 'checked' : ''"
            @click="
              offerList[index].buyed = !offerList[index].buyed
              $forceUpdate()
            "
          >
            √
          </div>
        </div>
      </div>
      <div class="offer-to-cart">
        <div class="total">组合总价：</div>
        <div class="price">${{ getOfferTotalPrice() }}</div>
        <div class="button" @click="addOfferToCart()">加入购物车</div>
      </div>

      <div class="tab">
        <div
          class="item"
          @click="showType = 1"
          :class="showType == 1 ? 'active' : ''"
        >
          基础信息
        </div>
        <div
          class="item"
          @click="showType = 2"
          :class="showType == 2 ? 'active' : ''"
        >
          商品详情
        </div>
        <div
          class="item"
          @click="showType = 3"
          :class="showType == 3 ? 'active' : ''"
        >
          资料下载
        </div>
      </div>
      <div class="content">
        <div class="base" v-if="showType == 1">
          <div class="item">
            <div class="left">产品介绍 :</div>
            <div class="right">
              <div class="line" v-html="product.productDescription"></div>
            </div>
            <div class="clear"></div>
          </div>
          <div class="item">
            <div class="left">特征及优点 :</div>
            <div class="right">
              <div class="line" v-html="product.productFeatures"></div>
            </div>
            <div class="clear"></div>
          </div>
          <div class="item">
            <div class="left">技术参数:</div>
            <div class="right">
              <div class="line" v-html="product.productTechnical"></div>
            </div>
            <div class="clear"></div>
          </div>
          <div class="item">
            <div class="left">尺寸参数:</div>
            <div class="right">
              <img
                :src="item"
                v-for="(item, index) in product.sizePicList"
                :key="index"
                class="image"
                mode="widthFix"
              />
            </div>
            <div class="clear"></div>
          </div>
          <div class="item">
            <div class="left">包装清单:</div>
            <div class="right">
              <img
                :src="item"
                v-for="(item, index) in product.packPicList"
                :key="index"
                class="image"
                mode="widthFix"
              />
            </div>
            <div class="clear"></div>
          </div>
          <div class="item">
            <div class="left">包装数据:</div>
            <div class="right">
              <div class="line" v-html="product.productDescription"></div>
            </div>
            <div class="clear"></div>
          </div>
        </div>
        <div
          class="detail"
          v-if="showType == 2"
          v-html="product.productDescEdit"
        ></div>
        <div class="download" v-if="showType == 3">
          <a :href="product.productPdf" target="_blank">PDF</a>
          <br />
          <a :href="product.direction" target="_blank">使用手册</a>
        </div>
        <div class="relate">
          <div class="title">相关产品推荐</div>
          <div class="list">
            <div class="item" v-for="(item, index) in relateList" :key="index">
              <img
                :src="item.attrList[0].proPicList[0]"
                class="pic"
                @click="
                  goto(
                    '../detail/detail?pname=' +
                      encodeURIComponent(pname) +
                      '&cname=' +
                      encodeURIComponent(cname) +
                      '&id=' +
                      item.id
                  )
                "
              />
              <div class="memo">SFDPA1-030-055-43</div>
              <div class="name">{{ item.name }}</div>
              <div class="price">${{ item.attrList[0].price }}</div>
            </div>
          </div>
        </div>
        <div class="clear" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      id: '',
      product: {},
      offerList: [],
      relateList: [],
      showType: 1,
      refresh: true,
      cartNum: 0,
      pname: '',
      cname: '',
      pid: ''
    }
  },
  onShow() {
    this.getCartList()
  },
  onLoad(e) {
    this.id = e.id || '1750706206776778753'
    this.pid = this.$route.query.pid
    this.pname = decodeURIComponent(this.$route.query.pname)
    this.cname = decodeURIComponent(this.$route.query.cname)
    this.getProduct()
  },
  methods: {
    getCartList() {
      uni.request({
        url: this.apiUrl + 'productApi/getCartList',
        method: 'GET',
        header: {
          Authorization: this.getToken()
        },
        success: res => {
          this.cartList = res.data.data
          this.cartNum = this.cartList.length
        },
        complete: () => {}
      })
    },

    addToCart(num, productId, productSeriesId) {
      uni.request({
        url: this.apiUrl + 'productApi/addOrUpdateToCart',
        method: 'POST',
        header: {
          Authorization: this.getToken()
        },
        data: {
          num,
          productId,
          productSeriesId
        },
        success: res => {
          if (num == 0) {
            this.showToast('已删除')
          } else {
            this.showToast('已添加购物车')
          }
          this.getCartList()
        },
        complete: () => {}
      })
    },
    onInput(e, index) {
      this.offerList[index].buyNumber = e.detail.value
    },
    onInputP(e) {
      this.product.buyNumber = e.detail.value
    },
    selectAttr(index) {
      this.$set(this.product, 'buyIndex', index)
      this.$set(this.product, 'showPicIndex', 0)
      this.getOfferList()
      this.$forceUpdate()
    },
    getProduct() {
      uni.request({
        url: this.apiUrl + 'productApi/getProductItem?id=' + this.id,
        method: 'GET',
        success: res => {
          this.product = res.data.data
          this.product.buyNumber = 1
          this.product.buyIndex = 0
          this.product.showPicIndex = 0
          this.getOfferList()
          this.getRelateList()
        },
        complete: () => {}
      })
    },
    getOfferList() {
      uni.request({
        url:
          this.apiUrl +
          `productApi/getProductModel?productId=${
            this.product.attrList[this.product.buyIndex].id
          }&type=1&page=1&limit=6`,
        method: 'GET',
        success: res => {
          if (res.data.code == 0) {
            this.offerList = res.data.data

            const self = this
            this.offerList.map((item, index) => {
              self.offerList[index].buyNumber = 1
              self.offerList[index].buyed = false
              self.offerList[index].buyIndex = 0
            })
            this.$forceUpdate()
          }
        },
        complete: () => {}
      })
    },
    getOfferTotalPrice() {
      let total = 0
      const selectedList = this.offerList.filter(item => item.buyed)
      selectedList.map(item => {
        total = total + item.buyNumber * item.attrList[item.buyIndex].price
      })
      return total
    },
    addOfferToCart() {
      const self = this
      const selectedList = this.offerList.filter(item => item.buyed)

      if (selectedList.length == 0) {
        this.showToast('请选择商品')
        return false
      }
      selectedList.map(item => {
        self.addToCart(item.buyNumber, item.attrList[item.buyIndex].id, item.id)
      })
    },
    getRelateList() {
      uni.request({
        url:
          this.apiUrl +
          `productApi/getProductModel?productId=${this.product.id}&type=2&page=1&limit=6`,
        method: 'GET',
        success: res => {
          if (res.data.code == 0) {
            this.relateList = res.data.data
          }
        },
        complete: () => {}
      })
    }
  }
}
</script>

<style lang="scss">
.main {
  width: 1200px;
  margin: 0 auto;
  margin-top: 64px;

  .pic {
    width: 548px;
    float: left;

    .big-pic {
      width: 548px;
      height: 548px;
      background-color: white;
    }

    .nav {
      margin-top: 12px;
      height: 108px;
      position: relative;

      .left {
        position: absolute;
        left: 0;
        width: 32px;
        height: 64px;
        top: 22px;
      }

      .mid {
        width: 468px;
        height: 108px;
        overflow-y: hidden;
        overflow-x: hidden;
        position: absolute;
        top: 0;
        left: 40px;
        white-space: nowrap;

        .item {
          width: 108px;
          height: 108px;
          display: inline-block;
          margin-right: 12px;
          border: 1px solid rgba(34, 34, 34, 0.2);
        }

        .item.active {
          border: 1px solid #222222;
        }
      }

      .right {
        position: absolute;
        right: 0;
        width: 32px;
        height: 64px;
        top: 22px;
      }
    }
  }

  .info {
    width: 588px;
    float: right;

    .name {
      font-size: 32px;
      font-weight: 500;
      color: #222222;
      line-height: 32px;
      margin-top: 12px;
    }

    .memo {
      font-size: 14px;
      font-weight: 400;
      color: #535353;
      line-height: 14px;
      margin-top: 24px;
    }

    .price {
      font-size: 24px;
      font-weight: 600;
      color: #ff590f;
      line-height: 24px;
      margin-top: 32px;
    }

    .line {
      width: 588px;
      height: 1px;
      border: 1px solid #dddddd;
      margin-top: 23px;
    }

    .attrs {
      font-size: 14px;
      font-weight: 400;
      color: #535353;
      margin-top: 17px;

      .item {
        line-height: 2;
      }
    }

    .color-title {
      font-size: 14px;
      font-weight: 600;
      color: #535353;
      line-height: 14px;
      margin-top: 20px;
    }

    .colors {
      // height: 54px;
      margin-top: 16px;
      margin-right: -52px;

      .item {
        display: inline-flex;
        width: 184px;
        height: 54px;
        margin-right: 18px;
        border: 1px solid #dddddd;
        font-size: 14px;
        font-weight: 400;
        color: #535353;
        line-height: 14px;
        margin-top: 10px;
        align-items: center;

        .pic {
          width: 46px;
          height: 46px;
          margin-left: 4px;
          background-color: white;
          margin-right: 14px;
        }
      }

      .item.active {
        border: 2px solid #ff590f;
      }
    }

    .dot-line {
      width: 588px;
      height: 1px;
      border: 1px dashed #dddddd;
      margin-top: 23px;
    }

    .buy {
      margin-top: 32px;
      position: relative;
      height: 54px;

      .number {
        display: inline-flex;
        align-items: center;
        position: absolute;
        top: 0;
        left: 0;

        .minus,
        .plus {
          width: 54px;
          height: 54px;
          border: 1px solid #dddddd;
          text-align: center;
          display: inline-block;
          line-height: 48px;
          font-size: 28px;
        }

        .total {
          height: 54px;
          width: 76px;
          line-height: 40px;
          border-top: 1px solid #dddddd;
          border-bottom: 1px solid #dddddd;
          text-align: center;
          display: inline-block;
          font-size: 20px;
          font-weight: 500;
          color: #17183b;
        }

        .disable {
          background-color: #dddddd;
        }
      }

      .price-title {
        font-size: 14px;
        font-weight: 400;
        color: #222222;
        line-height: 14px;
        position: absolute;
        top: 4px;
        left: 270px;
      }

      .price {
        font-size: 24px;
        font-weight: 500;
        color: #ff590f;
        line-height: 24px;
        position: absolute;
        bottom: 4px;
        left: 270px;
      }

      .button {
        width: 184px;
        height: 54px;
        line-height: 54px;
        text-align: center;
        background: #ff590f;
        position: absolute;
        right: 0;
        top: 0;
        font-size: 16px;
        font-weight: 500;
        color: #ffffff;
      }
    }
  }

  .long-line {
    width: 100%;
    height: 1px;
    border: 1px solid #dddddd;
    margin-top: 47px;
  }

  .offer-title {
    font-size: 20px;
    font-weight: 600;
    color: #000000;
    line-height: 20px;
    margin-top: 48px;
  }

  .offer-list {
    height: 324px;
    width: 100%;
    margin-top: 34px;
    margin-right: -15px;
    white-space: nowrap;

    .item {
      width: 190px;
      height: 324px;
      background: #ffffff;
      border: 1px solid #dddddd;
      display: inline-block;
      margin-right: 12px;
      position: relative;

      .checkbox {
        width: 16px;
        height: 16px;
        background: #ffffff;
        border: 1px solid #dddddd;
        position: absolute;
        top: 8px;
        right: 8px;
        text-align: center;
        line-height: 16px;
        color: #ffffff;
        font-size: 12px;
      }

      .checkbox.checked {
        background-color: #ff590f;
        border: 1px solid #ff590f;
      }

      .pic {
        width: 166px;
        height: 166px;
        position: absolute;
        top: 12px;
        left: 12px;
        background-color: #ffffff;
      }
      .name {
        position: absolute;
        width: 100px;
        top: 190px;
        text-align: center;
        font-size: 14px;
        font-weight: 400;
        color: #222222;
        left: 45px;
        word-wrap: break-word;
        word-break: break-all;
        white-space: normal;
        line-height: 1.5;
      }
      .price {
        font-size: 16px;
        font-weight: 600;
        color: #222222;
        line-height: 16px;
        width: 100%;
        text-align: center;
        position: absolute;
        top: 236px;
      }
      .color {
        width: 62px;
        height: 32px;
        background: #ffffff;
        border: 1px solid #dddddd;
        position: absolute;
        left: 12px;
        bottom: 20px;
        padding-left: 8px;
        font-size: 12px;
        font-weight: 400;
      }
      .number {
        display: inline-flex;
        align-items: center;
        position: absolute;
        right: 12px;
        bottom: 20px;

        .minus,
        .plus {
          width: 32px;
          height: 32px;
          border: 1px solid #dddddd;
          text-align: center;
          display: inline-block;
          line-height: 28px;
          font-size: 20px;
        }

        .total {
          height: 32px;
          width: 32px;
          line-height: 32px;
          border-top: 1px solid #dddddd;
          border-bottom: 1px solid #dddddd;
          text-align: center;
          display: inline-block;
          font-size: 14px;
          font-weight: 500;
          color: #17183b;
        }

        .disable {
          background-color: #dddddd;
        }
      }
    }

    .item:hover {
      border: 2px solid #ff590f;
    }
  }

  .offer-to-cart {
    height: 54px;
    margin-top: 32px;
    position: relative;

    .total {
      font-size: 14px;
      font-weight: 400;
      color: #222222;
      line-height: 14px;
      position: absolute;
      top: 4px;
      right: 227px;
    }
    .price {
      font-size: 24px;
      font-weight: 500;
      color: #ff590f;
      line-height: 24px;
      position: absolute;
      bottom: 4px;
      right: 227px;
    }
    .button {
      width: 184px;
      height: 54px;
      line-height: 54px;
      text-align: center;
      background: #ff590f;
      font-size: 16px;
      font-weight: 500;
      color: #ffffff;
      position: absolute;
      top: 0;
      right: 0;
    }
  }

  .tab {
    margin-top: 64px;
    width: 100vw;
    height: 54px;
    background: #f5f5f7;
    box-shadow: 0px 1px 0px 0px #dddddd, 0px -1px 0px 0px #dddddd;
    margin-left: calc(600px - 50vw);
    padding-left: calc(50vw - 600px);

    .item {
      width: 180px;
      height: 55px;
      text-align: center;
      line-height: 55px;
      font-size: 18px;
      font-weight: 400;
      color: #000000;
      display: inline-block;
    }
    .item.active {
      background-color: #ffffff;
      border-bottom: 1px solid #ffffff;
    }
  }

  .content {
    min-height: 1736px;
    margin-top: 17px;
    .detail,
    .base,
    .download {
      width: 840px;
      float: left;
    }

    .base .item {
      margin-top: 48px;
      padding-bottom: 48px;
      box-shadow: 0px 1px 0px 0px #dddddd;
      .left {
        font-size: 18px;
        font-weight: 600;
        color: #303030;
        line-height: 18px;
        float: left;
      }
      .right {
        width: 628px;
        float: right;

        font-size: 16px;
        font-weight: 400;
        color: #6c6c6c;
        line-height: 24px;

        .image {
          width: 627px;
        }

        .line {
          width: 100%;
          .l {
            float: left;
            width: 195px;
            display: inline-block;
          }
          .r {
            float: right;
            width: 391px;
            display: inline-block;
          }
        }
      }
    }

    .relate {
      width: 340px;
      height: 1635px;
      background: #ffffff;
      box-shadow: -1px 0px 0px 0px #dddddd;
      float: right;

      .title {
        width: 256px;
        height: 58px;
        background: #f5f5f7;
        line-height: 58px;
        text-align: center;
        font-size: 18px;
        font-weight: 600;
        color: #393939;
        margin-top: 31px;
        margin-left: 42px;
      }

      .list {
        margin-top: 36px;
        margin-left: 42px;

        .item {
          width: 256px;
          border: 1px solid #dddddd;
          margin-bottom: 24px;

          .pic {
            width: 240px;
            height: 240px;
            background-color: #ffffff;
            margin-top: 8px;
            margin-left: 8px;
            display: block;
          }

          .memo {
            margin-top: 268px;
            font-size: 14px;
            font-weight: 400;
            color: #535353;
            line-height: 14px;
            text-align: center;
          }
          .name {
            margin-top: 6px;
            word-wrap: break-word;
            word-break: break-all;
            white-space: normal;
            text-align: center;
            width: 150px;
            margin-left: 53px;
            font-size: 16px;
            font-weight: 600;
            color: #222222;
            line-height: 16px;
          }
          .price {
            font-size: 16px;
            font-weight: 600;
            color: #ff6600;
            line-height: 16px;
            margin-top: 12px;
            text-align: center;
            margin-bottom: 24px;
          }
        }
      }
    }
  }
}
</style>
